from PyQt5.QtSql import QSqlDatabase, QSqlQuery,QSqlTableModel
from datetime import date
import getpath
import logging
db=None
def init_db():
    global db
    db = QSqlDatabase.addDatabase("QODBC")
    SERVER = '10.0.108.107'
    USERNAME = 'lims'
    PASSWORD = 'QmPz123@me'
    DATABASE = 'caiji'
    connectionString = f'DRIVER={{ODBC Driver 18 for SQL Server}};SERVER={SERVER};DATABASE={DATABASE};ENCRYPT=yes;TrustServerCertificate=yes;UID={USERNAME};PWD={PASSWORD}'
    db.setDatabaseName(connectionString)
    db.open()
def test1():
    sql_cmd("select * from bj_syy")
def sql_cmd(cmd):
    q = QSqlQuery()
    q.prepare(cmd)
    r = q.exec_()
    if not r:
        logging.info(q.lastError())    
    else:
        while (q.next()):
            x1= q.value(0)
            print(x1)
    q.finish()
if __name__=="__main__":
    init_db()
    print(dir(db),db,type(db))
    test1()